package com.bigstonecontainer.training.springboot.frm.server.sys.controller;

import com.bigstonecontainer.training.springboot.frm.server.core.domain.Result;
import com.bigstonecontainer.training.springboot.frm.server.sys.domain.SysUser;
import com.bigstonecontainer.training.springboot.frm.server.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器。
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户列表查询
     *
     * @return 用户列表
     */
    @PostMapping("/list")
    public Result<List<SysUser>> getList() {
        List<SysUser> list = sysUserService.getList();
        return Result.success(list);
    }

    /**
     * 获取单个用户信息
     * @param id 用户ID
     * @return 单个用户信息
     */
    @GetMapping("/{id}")
    public Result<SysUser> getOne(@PathVariable("id") Long id) {
        SysUser one = sysUserService.getOne(id);
        return Result.success(one);
    }

    /**
     * 新建一个用户
     * @param user 用户
     * @return
     */
    @PostMapping
    public Result<Integer> create(@RequestBody SysUser user) {
        Integer result = sysUserService.create(user);
        return Result.success(result);
    }

    /**
     * 修改一个用户
     * @param user 用户
     * @return
     */
    @PutMapping
    public Result<Integer> updateById(@RequestBody SysUser user) {
        Integer result = sysUserService.updateById(user);
        return Result.success(result);
    }

    /**
     * 删除单个用户
     * @param id 用户ID
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        Integer result = sysUserService.deleteById(id);
        return Result.success(result);
    }
}
